	import model.UserModel;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;
	import mx.rpc.xml.SimpleXMLEncoder;
	
	import nrb.utils.DateHandler;
	import nrb.utils.XMLHelper;
	
	[Bindable] public var collection_user_roles:ArrayCollection;
	[Bindable] public var xml_users:XML;
	[Bindable] private var label_title_window:String;
	private var helper_xml:XMLHelper;
	public var user_model:UserModel;
	private var dates:DateHandler;
	private var pA:Object;
	private var current_screen:String;
	

	public function func_init(): void {
		collection_user_roles = new ArrayCollection([{label:'Admin', data:'Admin'}, {label:'User', data:'User'}]);
		xml_users = new XML();
		helper_xml = new XMLHelper();
		user_model = new UserModel();
		dates = new DateHandler();
		pA = new Object();
		
		pA = parentApplication;
		current_screen = 'NEW';
		label_title_window = 'Add User';
		btn_save_and_add.visible = true;
		
		//setfocus
		txt_userid.setFocus();
	}
	
	
	public function func_set_vars(obj:Object): void {
		if(obj.xml_users)
			xml_users = obj.xml_users;
		if(obj.helper_xml)
			helper_xml = obj.helper_xml;
		if(obj.user_model){
			current_screen = 'EDIT';
			label_title_window = 'Edit User';
			user_model = obj.user_model;
			func_init_fields();
			btn_save_and_add.visible = false;
		}
	}
	
	private function func_save(flag:String=null): void {
		if(current_screen == 'NEW'){
			func_new(flag);
		}else{
			func_edit();
		}
	}
	
	private function func_new(flag:String=null): void {
		//check fields
		if(func_check_fields()){
			//check if there is no duplicate
			if(xml_users.user.(userid == txt_userid.text).userid == undefined){
				//set user attributes
				user_model = new UserModel();
				user_model.userid = txt_userid.text;
				user_model.password = txt_password.text;
				user_model.username = txt_username.text;
				user_model.userrole = cb_userrole.selectedItem.data;
				user_model.userdescription = txt_userdescription.text;
				user_model.datecreated = dates.get_full_date('YYYY-MM-DD HH:NN:SS');
				user_model.datemodified = dates.get_full_date('YYYY-MM-DD HH:NN:SS');
				user_model.createdby = pA.LOGGED_USER_ID;
				user_model.modifiedby = pA.LOGGED_USER_ID;
				
				//object to user xml node
				var q_name:QName = new QName('user');
				var xml_doc:XMLDocument = new XMLDocument();
				var xml_encoder:SimpleXMLEncoder = new SimpleXMLEncoder(xml_doc);
				var xml_node:XMLNode = xml_encoder.encodeValue(user_model,q_name,xml_doc);
				var user_xml_node:XML = new XML(xml_doc.toString());		
				xml_users.appendChild(user_xml_node);
				helper_xml.func_write_xml(xml_users);
				
				if(flag != 'SAVE')
					func_close_popup();
			}else{
				Alert.show('User ID already exist');
			}
		}else{
			Alert.show('Fields Required');
		}
	}
	
	private function func_edit(): void {
		//check fields
		if(func_check_fields()){
			if(func_is_edit_allowed()){
				xml_users.user[user_model.index].userid = txt_userid.text;
				xml_users.user[user_model.index].password = txt_password.text;
				xml_users.user[user_model.index].username = txt_username.text;
				xml_users.user[user_model.index].userrole = cb_userrole.selectedItem.data;
				xml_users.user[user_model.index].userdescription = txt_userdescription.text;
				xml_users.user[user_model.index].datemodified = dates.get_full_date('YYYY-MM-DD HH:NN:SS');
				xml_users.user[user_model.index].modifiedby = pA.LOGGED_USER_ID;
				helper_xml.func_write_xml(xml_users);
				//datagrid_users.dataProvider = xml_users.user;
				func_close_popup();
			}else{
				Alert.show('User ID already exist');
			}
			
		}else{
			Alert.show('Fields Required');
		}
	}
	
	private function func_delete(): void {
		Alert.show("Are you sure you want to delete this user?", null,Alert.OK | Alert.CANCEL, this,func_delete_listener);
	}
	
	
	private function func_delete_listener(eventObj:CloseEvent): void {
		// Check to see if the OK button was pressed.
        if (eventObj.detail==Alert.OK) {
            if(xml_users.user[user_model.index].userid != pA.LOGGED_USER_ID){
				delete xml_users.user[user_model.index];
				helper_xml.func_write_xml(xml_users);
				func_clear_form();
			}else{
				Alert.show('Cannot Delete Logged-in User');
			} 
        }

	}
	
	private function func_is_edit_allowed(): Boolean {
		//check first if user id is not same with old, then check for duplicate, else proceed with edit
		if(xml_users.user[user_model.index].userid != txt_userid.text){
			if(xml_users.user.(userid == txt_userid.text).userid == undefined){
				return true;
			}else{
				return false;
			}
		}
		
		return true;
	}
	
	private function func_check_fields(): Boolean {
		if(txt_userid.text == '' || txt_password.text == ''){
			txt_userid.setFocus();
			txt_password.setFocus();
			return false;
		}
		
		return true;
	}
	
	private function func_clear_form(): void {
			txt_userid.text = '';
			txt_password.text = '';
			txt_username.text = '';
			cb_userrole.selectedIndex = 0;
			txt_userdescription.text = '';
	}
	
	private function func_init_fields(): void {
		//show data in fields
		if(txt_userid){
			txt_userid.text = user_model.userid;
			txt_userid.errorString = '';
		}
		if(txt_password){
			txt_password.text = user_model.password;
			txt_password.errorString = '';
		}
		if(txt_username)
			txt_username.text = user_model.username;
		if(cb_userrole){
			if(user_model.userrole == 'Admin')
				cb_userrole.selectedIndex = 0;
			else
				cb_userrole.selectedIndex = 1;
		}
		if(txt_userdescription)
			txt_userdescription.text = user_model.userdescription;
	}
	
	private function func_close_popup(): void {
		PopUpManager.removePopUp(this);
	}